<?php

include_once dirname(dirname(__FILE__)) . '/include/common.php';
$core = new core();
$customer_order_id = isset($_POST["customer_order_id"]) ? intval($_POST["customer_order_id"]) : 0;
$customer_order = $core->getZenCustomerOrderById($customer_order_id);
if (!$customer_order) {
    echo json_encode(array('type' => 1, 'msg' => "Đợt hàng này không tồn tại!"));
    die;
}
$payment_id = isset($_POST["payment_id"]) ? intval($_POST["payment_id"]) : 0;
$payment = isset($_POST["payment"]) ? $_POST["payment"] : 0;
$type_payment = isset($_POST["type_payment"]) ? intval($_POST["type_payment"]) : 1;
$description = isset($_POST["description"]) ? addslashes(trim($_POST["description"])) : '';
$payment_date = isset($_POST["payment_date"]) ? addslashes(trim($_POST["payment_date"])) : '';
$payment_date = date('Y-m-d', strtotime($payment_date));
$payment = floatval(preg_replace('/[^\d.]/', '', $payment));
$payment = $payment * 1000;
if ($payment == 0) {
    echo json_encode(array('type' => 1, 'msg' => "Vui lòng nhập số tiền thanh toán!"));
    die;
}
if ($type_payment != 1 && $type_payment != 2 && $type_payment != 3 && $type_payment != 4) {
    $type_payment = 1;
}
if ($type_payment == 3 || $type_payment == 4) {
    $payment = $payment * (-1);
}
$message = '';
if ($payment_id > 0) {
    $data_payment = $core->getPaymentById($payment_id);
    if ($data_payment) {
        $new_payment = $payment - $data_payment->payment;
        $payment = core::roundingMoney($payment);

        $core->db->query("UPDATE zen_payment SET payment='$payment',type_payment=$type_payment,description='$description',date_payment='$payment_date'
             WHERE id=$payment_id");
        $message = "Update payment thành công!";
    } else {
        echo json_encode(array('type' => 1, 'msg' => "payment không tồn tại!"));
        die;
    }
} else {
    $new_payment = $payment;
    $payment = core::roundingMoney($payment);

    $core->db->query("INSERT INTO zen_payment (customer_order_id,payment,type_payment,description,date_payment)
                        VALUES($customer_order_id,$payment,$type_payment,'$description','$payment_date')");

    if ($core->db->insert_id > 0) {
        $message = "Thêm payment thành công!";
    } else {
        echo json_encode(array('type' => 1, 'msg' => "Thêm payment thất bại!"));
        die;
    }
}
//update total_payment,total_current_money
$total_payment = $customer_order->total_payment + $new_payment;
$total_payment = core::roundingMoney($total_payment);
$total_current_money = $customer_order->total_current_money + $new_payment;
$total_current_money = core::roundingMoney($total_current_money);

$core->db->query("UPDATE zen_customer_order SET total_payment=$total_payment,
                        total_current_money= $total_current_money
                            WHERE customer_order_id=$customer_order_id");
$html = $core->renderPayment($customer_order_id);

echo json_encode(array('type' => 0, 'msg' => $message, 'html' => $html));
die;
?>